<?php
/**
 * 平台账号
 * User: bgl
 * Date: 2022/9/29
 * Time: 17:14
 */

namespace App\Admin\Controllers\Basics;

use App\Models\Basics\AmazonAccount;
use App\Models\Basics\SysArea;
use App\Models\Basics\SysPlatAccount;
use App\Models\Basics\SysPlat;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;

class SysPlatAccountController extends AdminController{

    protected $title = '平台店铺账号';

    protected function grid(){

        $grid = new Grid(new SysPlatAccount());

        $grid->column('id', 'ID');
        $grid->column('plat_id', '店铺平台')->display(function ($value){
            return SysPlat::getSysPlatTitle($value);
        });
        $grid->column('store', '店铺名称');
        $grid->column('account', '店铺账号');
        $grid->column('created_at', '创建时间');

        $grid->expandFilter();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableDelete();
        });
        $grid->disableRowSelector();
        $grid->disableExport();

        $grid->filter(function($filter){
            $filter->column(1/3, function ($filter){
                $filter->equal('account','店铺账号');
                $filter->equal('plat_id', '店铺平台')->select(SysPlat::options());
            });
        });

        return $grid;
    }

    protected function form() {

        $form = new Form(new SysPlatAccount());

        $form->hidden('id');
        $form->text('store', '平台店铺名称')->rules('required');
        $form->text('account', '平台店铺账号')
            ->creationRules(['required', "unique:b_sys_plat_account"],['required' => '账号必填','unique' => '账号已存在'])
            ->updateRules(['required', "unique:b_sys_plat_account,account,{{id}}"],['required' => '账号必填','unique' => '账号已存在']);

        $form->radio('plat_id', '账号平台')
            ->options(SysPlat::options())
            ->when(SysPlat::PLAT_AMAZON, function (Form $form) {
                //amazon_account
                $form->text('amazon_account.store', 'Amazon店铺');
                $form->text('amazon_account.account', 'Amazon账号');
                $form->multipleSelect('amazon_account.area_id', '站点')->options(SysArea::options())->required()->rules("required");
                $form->select('amazon_account.aws_region', '地区')->options(AmazonAccount::$_awsRegion);
                $form->radio('amazon_account.api','API')->options(AmazonAccount::$_api)->default(0);
                $form->radio('amazon_account.status','状态')->options(AmazonAccount::$_status)->default(1);
                //API字段------
                $form->text('amazon_account.merchant', '卖家编号')->help('亚马逊API字段-授权后生成');
                $form->text('amazon_account.auth_at', '授权时间')->help('亚马逊API字段-授权后生成');
                $form->text('amazon_account.refresh_token','授权Token')->help('亚马逊API字段-授权后生成');
                $form->hidden('amazon_account.auth_status')->default(0);


            })->when(SysPlat::PLAT_SHOPIFY, function (Form $form) {
                //shopify_account
                $form->text('shopify_account.store', 'Shopify店铺');
                $form->text('shopify_account.account', 'Shopify账号');
                $form->text('shopify_account.token', '令牌');
                $form->text('shopify_account.key', '秘钥');
                $form->text('shopify_account.secret', '授权码');
                $form->text('shopify_account.site', '授权网址');

            })->when(SysPlat::PLAT_RAKUTEN, function (Form $form) {
                //rakuten_account
                $form->text('rakuten_account.store', 'Rakuten店铺');
                $form->text('rakuten_account.account', 'Rakuten账号');
                $form->text('rakuten_account.authorization', '授权秘钥');
                $form->text('rakuten_account.site', '授权网址');
                $form->date('rakuten_account.expire_time', '授权到期时间');
            })->readonly();

       /* $form->radio('plat_id', '账号平台')
            ->options(SysPlat::options())
            ->when(SysPlat::PLAT_AMAZON, function (Form $form) {
                //amazon_account
                $form->text('merchant', '卖家编号');
                $form->date('auth_at', '授权时间');

            })->when(SysPlat::PLAT_SHOPIFY, function (Form $form) {
                //shopify_account
                $form->text('token', '令牌');
                $form->text('key', '秘钥');
                $form->text('secret', '授权码');
                $form->text('site', '授权网址');

            })->when(SysPlat::PLAT_RAKUTEN, function (Form $form) {
                //rakuten_account
                $form->text('authorization', '授权秘钥');
                $form->text('site', '授权网址');
                $form->date('expire_time', '授权到期时间');
            });*/

        $form->radio('status', '是否启用')->options([
            '1' => '是',
            '2' => '否'
        ])->default(1);

        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
            $tools->disableDelete();
        });

        return $form;
    }

}
